package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.AdminRoleDto;
import com.have.blog.admin.service.AdminRoleService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色相关接口", tags = {"角色相关接口"})
@Slf4j
public class AdminRoleController {
    @Autowired
    private AdminRoleService roleService;

    @ApiOperation(value = "获取角色信息列表", notes = "获取角色信息列表")
    @PostMapping("/getList")
    public Wrapper<CommonPage<AdminRoleDto>> getList(@Validated({GetList.class}) @RequestBody AdminRoleDto roleDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取角色信息列表");
        List<AdminRoleDto> list = roleService.getList(roleDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @OperationLogger("新增角色")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody AdminRoleDto roleDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return roleService.add(roleDto);
    }

    @OperationLogger("编辑角色")
    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody AdminRoleDto roleDto, BindingResult result) {
        System.out.println(roleDto);
        ThrowableUtils.checkParamArgument(result);
        return roleService.edit(roleDto);
    }

    @OperationLogger("删除角色")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping("/delete")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody AdminRoleDto roleDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return roleService.delete(roleDto);
    }
}
